<template>
  <!-- 执行记录 -->

    <div class="expricingTaskRecord" >
      <span class= "formTitle">
        <i class="vertical-bar"></i>重定价定价任务监控
      </span>

      <el-form :model="searchForm" ref="exRecordRef" label-width="140px" label-position="right">
        <el-row :gutter="30">
          <el-col :span="8">
            <el-form-item label="任务编号：" prop="taskCode">
              <el-input v-model="searchForm.taskCode" style="width:150px" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务名称：" prop="taskName">
              <el-input v-model="searchForm.taskName" style="width:150px" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数据日期：" prop="dataDate">
              <el-date-picker
                v-model="searchForm.dataDate"
                type="date"
                style="width:150px;"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align:center;padding:10px">
        <el-button size="mini" @click="searchFunc">查询</el-button>
        <el-button size="mini" @click="searchForm.dataDate = ''">重置</el-button>
      </div>
      <el-row class="handleBox">
        <!-- 左侧按钮 -->
        <el-col :span="18">
          <el-button @click="expExcel()">导出</el-button>
          <el-button @click="toSubRecord()">子任务</el-button>
          <el-button @click="goBack()">返回</el-button>
        </el-col>
      </el-row>
      <el-table ref="multipleTable" :data="tableData" style="width: 100%" @row-click="rowClick">
        <el-table-column width="55" type="selection"  />

        <el-table-column prop="pricingCode" label="定价编号" show-overflow-tooltip />
        <el-table-column prop="dataDate" label="数据日期"
        :formatter="formatterTime" show-overflow-tooltip />
        <el-table-column prop="sceneCode" label="情景编号" show-overflow-tooltip />
        <el-table-column prop="sceneVersion" label="情景版本" show-overflow-tooltip />
        <el-table-column prop="startTm" label="开始时间" show-overflow-tooltip />
        <el-table-column prop="endTm" label="结束时间" show-overflow-tooltip />
        <el-table-column prop="successNum" label="成功记录" show-overflow-tooltip />
        <el-table-column prop="failNum" label="失败记录" show-overflow-tooltip />
        <el-table-column prop="status" label="执行记录" show-overflow-tooltip />
      </el-table>
        <pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-size="searchForm.size"
        @refresh="searchFunc"
        :total="total"
      ></pagination>

    </div>
   

</template>

<script>
import { getDict } from "@/api/getDict";
import { pricingTask,expricing } from "@/api/index";
export default {
  components: {
  },
  props: {
    //选中的值
    recordTaskCode: {
      type: String,
      default: ""
    },
    //显示页面属性
    exPricingShow: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      total: 0,
      //定价子任务表单数据
      subRecordModelData: [],
      titleName: "定价子任务",
      subRecord: false,
      searchForm: {
        taskCode: "",
        taskName: "异常定价",
        dataDate: ""
      },
      tableData: []
    };
  },
  watch: {
    code: function(newVal, oldVal) {
      if (newVal == false) {
      } else {
        this.searchFunc();
      }
    }
  },
  created() {
    this.searchForm.taskCode = this.recordTaskCode;
    this.searchFunc();
  },
  computed: {},
  mounted() {},
  methods: {
    handleSizeChange(val) {
      this.searchFunc.size = val;

      console.log(val, "每页数");
    },
    handleCurrentChange(val) {
      this.searchFunc.page = val;
      console.log(val, "当前页");
    },

    goBack() {
      this.$emit("closeExMonitor");
    },
    getstatus(data) {
      getDict(data).then(res => {
        if (res.code == 200) {
          this.statusOption = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //导出执行记录
    expExcel(){
         //判断是根据条件查询  还是根据选择查询
     
      let data = {
        taskCode: this.searchForm.taskCode,
        taskName: "异常定价",
        dataDate: this.searchForm.dataDate

      };
      expricing.expExcuteRecord(data).then(response => {
           console.log(response,"response");
        if (response.code == 500) {
          this.$message.error(response.message);
        } else if (response) {
          console.log(decodeURI(response.headers["content-disposition"]));
          var filename = decodeURI(
            response.headers["content-disposition"]
          ).split("=")[1];
          let contenttype =
            filename.substring(filename.lastIndexOf("."), filename.length) ===
            "xlsx"
              ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
              : "application/vnd.ms-excel;charset=utf-8";

          var blob = new Blob([response.data], {
            type: contenttype
          });
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, filename);
          } else {
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
          this.$message.success("导出成功");
        } else {
          this.$message.error(response.message || "获取失败");
        }
      });


    },
    toSubRecord() {
    let selections = this.$refs.multipleTable.selection;
    console.log(selections,"selections执行记录");
    if(selections.length !=1){
        return this.$message.warning("请选择一条数据,进行操作!");
    }
    let data = selections[0];
     let params = {
        taskName: this.searchForm.taskName,
        pricingCode: data.pricingCode,
        dataDate: data.dataDate,
        taskCode: this.searchForm.taskCode
      };

        this.$emit("toSubRecord",params);
    },
    searchFunc() {
      let data = this.$clone(this.searchForm);
      console.log(data, "执行记录页面加载的时候传递的参数");
      pricingTask.findAllRecord(data).then(res => {
        console.log(res, "执行记录页面加载的时候查出来的数据==============");
        if (res.code == 200) {
          this.tableData = res.datas.rows;
        }
      });
    },

    //点击行选中
    authorizeModelFor(row) {
      if (this.$refs.subRecordModelTable) {
        this.$refs.subRecordModelTable.clearSelection();
      }
      this.$refs.subRecordModelTable.toggleRowSelection(row, true);
    },
    rowClick(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
    },
     //时间转换
    formatterTime(row, column) {
      let ele = row[column.property];
      if (ele) return moment(ele).format("YYYY-MM-DD HH:mm:ss");
      else return "-";
    },
  }
};
</script>

<style lang="scss" scoped>
.el-col-3 {
  width: 40%;
}
.formTitle {
  font-size: 14px;
  font-weight: 700;
  display: block;
  margin-bottom: 10px;
}
</style>
